//package com.joshua.spring.datasource.autoconfigure;
//import lombok.AllArgsConstructor;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.config.BeanDefinition;
//import org.springframework.boot.autoconfigure.AutoConfigureBefore;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
//import org.springframework.boot.context.properties.EnableConfigurationProperties;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.context.annotation.Import;
//import org.springframework.context.annotation.Role;
////import org.springframework.data.transaction.ChainedTransactionManager;
//import org.springframework.jdbc.datasource.DataSourceTransactionManager;
//import org.springframework.transaction.PlatformTransactionManager;
//
//import javax.sql.DataSource;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * 动态数据源核心自动配置类
// *
// * @author TaoYu Kanyuxia
// * @see DynamicDataSourceProvider
// * @see DynamicDataSourceStrategy
// * @see DynamicRoutingDataSource
// * @since 1.0.0
// */
//@Slf4j
//@Configuration
//@AllArgsConstructor
//@EnableConfigurationProperties(DynamicDataSourceProperties.class)
//@AutoConfigureBefore(DataSourceAutoConfiguration.class)
//@Import(value = {DruidDynamicDataSourceConfiguration.class, DynamicDataSourceCreatorAutoConfiguration.class})
//@ConditionalOnProperty(prefix = DynamicDataSourceProperties.PREFIX, name = "enabled", havingValue = "true", matchIfMissing = true)
//public class DynamicDataSourceAutoConfiguration {
//
//    private final DynamicDataSourceProperties properties;
//
//    @Bean
//    @ConditionalOnMissingBean
//    public DynamicDataSourceProvider dynamicDataSourceProvider() {
//        Map<String, DataSourceProperty> datasourceMap = properties.getDatasource();
//        return new YmlDynamicDataSourceProvider(datasourceMap);
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public DataSource dataSource(DynamicDataSourceProvider dynamicDataSourceProvider) {
//        DynamicRoutingDataSource dataSource = new DynamicRoutingDataSource();
//        dataSource.setPrimary(properties.getPrimary());
//        dataSource.setStrict(properties.getStrict());
//        dataSource.setStrategy(properties.getStrategy());
//        dataSource.setProvider(dynamicDataSourceProvider);
//        dataSource.setP6spy(properties.getP6spy());
//        dataSource.setSeata(properties.getSeata());
//        return dataSource;
//    }
//
//    @Bean
//    @ConditionalOnClass(ChainedTransactionManager.class)
//    public PlatformTransactionManager transactionManager(DataSource dataSource) {
//        DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
//        Map<String, DataSource> currentDataSources = ds.getCurrentDataSources();
//        List<PlatformTransactionManager> platformTransactionManagers = new ArrayList<>();
//        for (DataSource item : currentDataSources.values()) {
//            DataSourceTransactionManager transactionManager = new DataSourceTransactionManager(item);
//            platformTransactionManagers.add(transactionManager);
//        }
//        return new ChainedTransactionManager(platformTransactionManagers.toArray(new PlatformTransactionManager[0]));
//    }
//
//    @Role(value = BeanDefinition.ROLE_INFRASTRUCTURE)
//    @Bean
//    @ConditionalOnMissingBean
//    public DynamicDataSourceAnnotationAdvisor dynamicDatasourceAnnotationAdvisor(DsProcessor dsProcessor) {
//        DynamicDataSourceAnnotationInterceptor interceptor = new DynamicDataSourceAnnotationInterceptor(properties.isAllowPublicOnly());
//        interceptor.setDsProcessor(dsProcessor);
//        DynamicDataSourceAnnotationAdvisor advisor = new DynamicDataSourceAnnotationAdvisor(interceptor);
//        advisor.setOrder(properties.getOrder());
//        return advisor;
//    }
//
//    @Bean
//    @ConditionalOnMissingBean
//    public DsProcessor dsProcessor() {
//        DsHeaderProcessor headerProcessor = new DsHeaderProcessor();
//        DsSessionProcessor sessionProcessor = new DsSessionProcessor();
//        DsSpelExpressionProcessor spelExpressionProcessor = new DsSpelExpressionProcessor();
//        headerProcessor.setNextProcessor(sessionProcessor);
//        sessionProcessor.setNextProcessor(spelExpressionProcessor);
//        return headerProcessor;
//    }
//
//}
